package com.miwzn.boot.modules.sys.web;

import com.miwzn.base.AbstractController;
import com.miwzn.boot.modules.sys.entity.dto.SysDeptDto;
import com.miwzn.boot.modules.sys.entity.po.SysDept;
import com.miwzn.boot.modules.sys.entity.qm.SysDeptQuery;
import com.miwzn.boot.modules.sys.service.SysDeptService;
import com.miwzn.comm.ResponseEntity;
import com.miwzn.comm.annotation.OperationLog;
import com.miwzn.comm.constant.MyConstant;
import com.miwzn.comm.exception.BsRException;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: wzn
 * @Date: 2019-01-14 14:48:23
 * @Description: www.miwzn.com mvc api
 */
@ResponseBody
@Controller
@RequestMapping("sys/dept")
public class SysDeptController extends AbstractController<SysDept, SysDeptDto, SysDeptQuery> {
    private static Logger logger = LoggerFactory.getLogger(SysDeptController.class);

    @Autowired
    private SysDeptService sysDeptService;

    /**
     * 根据ID获取信息
     *
     * @param id
     *
     * @return
     */
    @OperationLog(operation = "系统-部门", content = "根据ID获取部门")
    @RequiresPermissions("sys:dept:getById")
    @GetMapping("/getById/{id}")
    public ResponseEntity getById(@PathVariable("id") String id) {
        SysDeptDto sysDeptDto = sysDeptService.selectById(id);
        return new ResponseEntity("查询成功").put("data", sysDeptDto);
    }

    /**
     * 列表
     *
     * @param entity
     *
     * @return
     */
    @OperationLog(operation = "系统-部门", content = "获取部门列表")
    @RequiresPermissions("sys:dept:list")
    @GetMapping("/list")
    public ResponseEntity list(SysDeptQuery entity) {
        return super.listD(entity);
    }

    /**
     * 添加
     *
     * @param entity
     *
     * @return
     */
    @OperationLog(operation = "系统-部门", content = "添加部门")
    @RequiresPermissions("sys:dept:add")
    @PostMapping("add")
    public ResponseEntity add(@RequestBody SysDeptDto entity) {
        checkDept(entity);
        return super.add(entity);
    }

    /**
     * 更新
     *
     * @param entity
     *
     * @return
     */
    @OperationLog(operation = "系统-部门", content = "更新部门")
    @RequiresPermissions("sys:dept:update")
    @PutMapping("update")
    public ResponseEntity update(@RequestBody SysDeptDto entity) {
        checkDept(entity);
        updateParentIds(entity);
        return super.update(entity);
    }

    private void updateParentIds(SysDeptDto entity) {
        SysDept p = sysDeptService.selectByPrimaryKey(entity.getId());
        if (!p.getParentId().equalsIgnoreCase(entity.getParentId())) {

            SysDept query = new SysDept();
            query.setParentId(entity.getId());
            List<SysDept> pos = sysDeptService.select(query);
            if (CollectionUtils.isNotEmpty(pos)) {
                for (SysDept po : pos) {
                    po.setParentId(entity.getId());
                    po.setLevel(entity.getLevel() + 1);
                    po.setParentIds(entity.getParentIds() + "," + entity.getId());
                    sysDeptService.updateByPrimaryKeySelective(po);
                    SysDeptDto dto = sysDeptService.wrapD(po);
                    updateParentIds(dto);
                }
            }
        }
    }

    private void checkDept(SysDeptDto entity) {
        if (entity.getParentId() == null) {
            entity.setParentId("0");
            entity.setParentIds("0");
            entity.setLevel(1);
        }
        SysDeptQuery q = new SysDeptQuery();
        q.setCode(entity.getCode());
        q.setDelFlag(MyConstant.FLAG_N);
        q.setName(entity.getName());
        q.setDelFlag(MyConstant.FLAG_N);
        q.setParentId(entity.getParentId());
        List<SysDept> list = sysDeptService.select(q);
        //同一父级下不能有相同的CODE
        if (CollectionUtils.isNotEmpty(list)) {
            if (entity.getId() == null) throw new BsRException("同一组部门名称不能重复");
            for (SysDept s : list) {
                if (!s.getId().equalsIgnoreCase(entity.getId())) {
                    throw new BsRException("同一组部门名称不能重复");
                }
            }
        }
    }

    /**
     * 软删除
     *
     * @param id
     *
     * @return
     */
    @OperationLog(operation = "系统-部门", content = "删除部门")
    @RequiresPermissions("sys:dept:delFlag")
    @DeleteMapping("/delFlag/{id}")
    public ResponseEntity deleteFlag(@PathVariable("id") String id) {
        return super.deleteFlag(id);
    }

    /**
     * 软删除
     *
     * @param ids
     *
     * @return
     */
    @OperationLog(operation = "系统-部门", content = "删除部门")
    @RequiresPermissions("sys:dept:delFlag")
    @DeleteMapping("/delFlag")
    public ResponseEntity deleteFlag(@RequestBody String[] ids) {
        return super.deleteFlag(ids);
    }

    /**
     * 硬删除
     *
     * @param id
     *
     * @return
     */
    @OperationLog(operation = "系统-部门", content = "删除部门")
    @RequiresPermissions("sys:dept:del")
    @DeleteMapping("/del/{id}")
    public ResponseEntity delete(@PathVariable("id") String id) {
        return super.delete(id);
    }

    /**
     * 硬删除
     *
     * @param ids
     *
     * @return
     */
    @OperationLog(operation = "系统-部门", content = "删除部门")
    @RequiresPermissions("sys:dept:del")
    @DeleteMapping("/del")
    public ResponseEntity delete(@RequestBody String[] ids) {
        return super.delete(ids);
    }

}
